package stephencarmody.random_rotating_triangles;

import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;

import stephencarmody.k8.core.Logic;
import stephencarmody.k8.core.Application;
import stephencarmody.k8.util.Camera;

public class User extends Logic {
    
    private static final float movementSpeed = 0.00000001f;
    
    private static int dx, dy;
    private Camera camera;
    private long lastupdate;
    private long elapsed;
    
    /** Creates a new instance of Camera */
    public User() {
    	camera = Camera.getInstance();
    	setUpdateRate(20);
    }
    
    @Override
    public void update(long time) {
        // Calculate elapsed time
        elapsed = time - lastupdate;
        lastupdate = time;

        if ( Keyboard.isKeyDown( Keyboard.KEY_E ) )
        	camera.moveForward(movementSpeed*elapsed);
        else if ( Keyboard.isKeyDown( Keyboard.KEY_D ) )
        	camera.moveForward(-movementSpeed*elapsed);
        if ( Keyboard.isKeyDown( Keyboard.KEY_S ) )
        	camera.moveRight(-movementSpeed*elapsed);
        else if ( Keyboard.isKeyDown( Keyboard.KEY_F ) )
        	camera.moveRight(movementSpeed*elapsed);
        if ( Keyboard.isKeyDown( Keyboard.KEY_Q ) )
        	camera.moveUp(movementSpeed*elapsed);
        else if ( Keyboard.isKeyDown( Keyboard.KEY_A ) )
        	camera.moveUp(-movementSpeed*elapsed);
        
        dx = Mouse.getDX();
        dy = Mouse.getDY();
        
        if (dx != 0) {
        	camera.yaw(-(float)dx / 100);
        }
        
        if (dy != 0) {
        	camera.pitch(-(float)dy / 100);
        }
        
        while ( Keyboard.next() ) {
        	// Ignore keydown events
        	if ( Keyboard.getEventKeyState() ) continue;
        	
        	switch ( Keyboard.getEventKey() ) {
        		case Keyboard.KEY_T:
        			Main.feeder.pause();
        			break;
            	
        		case Keyboard.KEY_R:
        			ConstantRotation.pauseAll();
        			break;
        			
        		case Keyboard.KEY_ESCAPE:
        			Application.finish();
        			break;
            }
        }
    }
    
}
